home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / SocketInputStream.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  151 lines

  1. /*
  2.  * @(#)SocketInputStream.java    1.16 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.IOException;
  18. import java.io.FileInputStream;
  19.  
  20. /**
  21.  * This stream extends FileInputStream to implement a
  22.  * SocketInputStream. Note that this class should <b>NOT</b> be
  23.  * public.
  24.  *
  25.  * @version     1.16, 07/01/98
  26.  * @author    Jonathan Payne
  27.  * @author    Arthur van Hoff
  28.  */
  29. class SocketInputStream extends FileInputStream
  30. {
  31.     private boolean eof;
  32.     private SocketImpl impl;
  33.     private byte temp[] = new byte[1];
  34.  
  35.     /**
  36.      * Creates a new SocketInputStream. Can only be called
  37.      * by a Socket. This method needs to hang on to the owner Socket so
  38.      * that the fd will not be closed.
  39.      * @param impl the implemented socket input stream
  40.      */
  41.     SocketInputStream(SocketImpl impl) throws IOException {
  42.     super(impl.getFileDescriptor());
  43.     this.impl = impl;
  44.     }
  45.  
  46.     /** 
  47.      * Reads into an array of bytes at the specified offset using
  48.      * the received socket primitive. 
  49.      * @param b the buffer into which the data is read
  50.      * @param off the start offset of the data
  51.      * @param len the maximum number of bytes read
  52.      * @return the actual number of bytes read, -1 is
  53.      *          returned when the end of the stream is reached. 
  54.      * @exception IOException If an I/O error has occurred.
  55.      */
  56.     private native int socketRead(byte b[], int off, int len)
  57.     throws IOException;
  58.  
  59.     /** 
  60.      * Reads into a byte array data from the socket. 
  61.      * @param b the buffer into which the data is read
  62.      * @return the actual number of bytes read, -1 is
  63.      *          returned when the end of the stream is reached. 
  64.      * @exception IOException If an I/O error has occurred. 
  65.      */
  66.     public int read(byte b[]) throws IOException {
  67.     return read(b, 0, b.length);
  68.     }
  69.  
  70.     /** 
  71.      * Reads into a byte array <i>b</i> at offset <i>off</i>, 
  72.      * <i>length</i> bytes of data.
  73.      * @param b the buffer into which the data is read
  74.      * @param off the start offset of the data
  75.      * @param len the maximum number of bytes read
  76.      * @return the actual number of bytes read, -1 is
  77.      *          returned when the end of the stream is reached. 
  78.      * @exception IOException If an I/O error has occurred.
  79.      */
  80.     public int read(byte b[], int off, int length) throws IOException {
  81.     if (eof) {
  82.         return -1;
  83.     }
  84.     int n = socketRead(b, off, length);
  85.     if (n <= 0) {
  86.         eof = true;
  87.         return -1;
  88.     }
  89.     return n;
  90.     }
  91.  
  92.     /** 
  93.      * Reads a single byte from the socket. 
  94.      */
  95.     public int read() throws IOException {
  96.     if (eof) {
  97.         return -1;
  98.     }
  99.  
  100.      int n = read(temp, 0, 1);
  101.     if (n <= 0) {
  102.         return -1;
  103.     }
  104.     return temp[0] & 0xff;
  105.     }
  106.  
  107.     /** 
  108.      * Skips n bytes of input.
  109.      * @param n the number of bytes to skip
  110.      * @return    the actual number of bytes skipped.
  111.      * @exception IOException If an I/O error has occurred.
  112.      */
  113.     public long skip(long numbytes) throws IOException {
  114.     if (numbytes <= 0) {
  115.         return 0;
  116.     }
  117.     long n = numbytes;
  118.     int buflen = (int) Math.min(1024, n);
  119.     byte data[] = new byte[buflen];
  120.     while (n > 0) {
  121.         int r = read(data, 0, (int) Math.min((long) buflen, n));
  122.         if (r < 0) {
  123.         break;
  124.         }
  125.         n -= r;
  126.     }
  127.     return numbytes - n;
  128.     }
  129.  
  130.     /**
  131.      * Returns the number of bytes that can be read without blocking.
  132.      * @return the number of immediately available bytes
  133.      */
  134.     public int available() throws IOException {
  135.     return impl.available();
  136.     }
  137.  
  138.     /**
  139.      * Closes the stream.
  140.      */
  141.     public void close() throws IOException {
  142.     impl.close();
  143.     }
  144.  
  145.     /** 
  146.      * Overrides finalize, the fd is closed by the Socket.
  147.      */
  148.     protected void finalize() {}
  149. }
  150.  
  151.